//读取dist目录下的文件
const fs = require('fs');
const path = require('path');

const distDir = path.join(__dirname,'..', 'dist');


//provinces.json文件里面的code再数字后面补上0保持6位数
const provinces = require(path.join(distDir, 'provinces.json'));
const cities = require(path.join(distDir, 'cities.json'));
const areas = require(path.join(distDir, 'areas.json'));
const streets = require(path.join(distDir, 'streets.json'));
const villages  = require(path.join(distDir, 'villages.json'));


//provinces增加type属性
provinces.forEach(province => {
    province.type = '2';
    province.parent_code = 'CN';
    province.parent_name = '中国';
    province.code_path = province.code;
    province.name_path = province.name;
});

let sql = provinces.map(province => {
    return `INSERT INTO system_area (code, name, parent_code, parent_name, type, code_path, name_path) VALUES ('${province.code}', '${province.name}', '${province.parent_code}', '${province.parent_name}', '${province.type}', '${province.code_path}', '${province.name_path}');`;
}).join('\n');
//写入文件
fs.writeFileSync(path.join(distDir, 'provinces.sql'), sql);

cities.forEach(city => {
    city.type = '3';
    city.parent_code = city.provinceCode;
    //province_name 是省份 关联查provinces中的name
    const parent = provinces.find(p => p.code === city.parent_code);
    city.parent_name = parent.name;
    city.code_path = parent.code_path + ',' + city.code;
    city.name_path = parent.name_path + '/' + city.name;
});
areas.forEach(area => {
    area.type = '4';
    area.parent_code = area.cityCode;
    const parent = cities.find(c => c.code === area.parent_code);
    area.parent_name = parent.name;
    area.code_path = parent.code_path + ',' + area.code;
    area.name_path = parent.name_path + '/' + area.name;
}); 
streets.forEach(street => {
    street.type = '5';
    street.parent_code = street.areaCode;
    const parent = areas.find(a => a.code === street.parent_code);
    street.parent_name = parent.name;
    street.code_path = parent.code_path + ',' + street.code;
    street.name_path = parent.name_path + '/' + street.name;
});
villages.forEach(village => {
    village.type = '6';
    village.parent_code = village.streetCode;
    const parent = streets.find(s => s.code === village.parent_code);
    village.parent_name = parent.name;
    village.code_path = parent.code_path + ',' + village.code;
    village.name_path = parent.name_path + '/' + village.name;
}); 


//把cities拼接成sql可以执行的语句
 sql = cities.map(city => {
    return `INSERT INTO system_area (code, name, parent_code, parent_name, type, code_path, name_path) VALUES ('${city.code}', '${city.name}', '${city.parent_code}', '${city.parent_name}', '${city.type}', '${city.code_path}', '${city.name_path}');`;
}).join('\n');
//写入文件
fs.writeFileSync(path.join(distDir, 'cities.sql'), sql);

//把areas拼接成sql可以执行的语句
 sql = areas.map(area => {
    return `INSERT INTO system_area (code, name, parent_code, parent_name, type, code_path, name_path) VALUES ('${area.code}', '${area.name}', '${area.parent_code}', '${area.parent_name}', '${area.type}', '${area.code_path}', '${area.name_path}');`;
}).join('\n');
//写入文件
fs.writeFileSync(path.join(distDir, 'areas.sql'), sql); 

//把streets拼接成sql可以执行的语句
 sql = streets.map(street => {
    return `INSERT INTO system_area (code, name, parent_code, parent_name, type, code_path, name_path) VALUES ('${street.code}', '${street.name}', '${street.parent_code}', '${street.parent_name}', '${street.type}', '${street.code_path}', '${street.name_path}');`;
}).join('\n');
//写入文件
fs.writeFileSync(path.join(distDir, 'streets.sql'), sql);   

//把villages拼接成sql可以执行的语句
 sql = villages.map(village => {
    return `INSERT INTO system_area (code, name, parent_code, parent_name, type, code_path, name_path) VALUES ('${village.code}', '${village.name}', '${village.parent_code}', '${village.parent_name}', '${village.type}', '${village.code_path}', '${village.name_path}');`;
}).join('\n');
//写入文件
fs.writeFileSync(path.join(distDir, 'villages.sql'), sql);


